
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Administrator
 */
public class GameSprite extends GameCanvas implements Runnable {
    
    private LayerManager mLayerManager;
    private Sprite mPlayer;
    private Sprite playerBullet;
    int playerBulletY;
    int playerBulletX;
    int countBullet = 0;
    int x = 110;
    int y = 270;
    Image img;
    int w = getWidth();
    int h = getHeight();
    int speed = 5;
    int speedY = 5;
    int startframe;
    boolean stopPlayer = false;
    private Sprite enemy1;
    private int enemy1X = 5;
    private int enemy1Y = 0;
    private Sprite enemyhelicopter;
    private Sprite enemyhelicopter2;
    boolean visibleHeli = false;
    int helicopter1X = 20;
    int helicopter2X = 250;
    int helicopter1Y = 0;
    int helicopter2Y = 40;
    int count = 0;
    int co = 0;
    boolean fire = false;
    int[] frame = {0, 1, 2};
    
    public GameSprite() {
        super(true);
        playerBulletY = y - 10;
        playerBulletX = x + 7;
        setFullScreenMode(true);
        mLayerManager = new LayerManager();
        Graphics g = getGraphics();
        createPlayer(g);
        createEnemy1(g);
        //createBullet(g);
        Thread t = new Thread(this);
        t.start();
    }
    
    private void createPlayer(Graphics g) {
        try {
            Image mImage = Image.createImage("/nguoichoi.png");
            mPlayer = new Sprite(mImage, 25, 25);
            mPlayer.setPosition(x, y);
            mPlayer.setFrame(1);
            mPlayer.defineReferencePixel(12, 12);
            mLayerManager.append(mPlayer);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    protected void keyPressed(int keyCode) {
        System.out.println(keyCode);
    }
    
    private void createEnemy1(Graphics g) {
        co++;
        try {
            Image enemy1Image = Image.createImage("/maybay1.png");
            enemy1 = new Sprite(enemy1Image, 16, 16);
            enemy1.setPosition(enemy1X, enemy1Y);
            enemy1.setFrame(4);
            enemy1.defineReferencePixel(8, 8);
            mLayerManager.append(enemy1);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    private void createHelicopter(Graphics g) {
        try {
            if (visibleHeli == true) {
                count++;
                mLayerManager.remove(enemy1);
                Image enemyHe = Image.createImage("/maybay3.png");
                enemyhelicopter = new Sprite(enemyHe, 40, 37);
                enemyhelicopter.setPosition(helicopter1X, helicopter1Y);
                enemyhelicopter.setFrame(0);
                enemyhelicopter.defineReferencePixel(12, 12);
                mLayerManager.append(enemyhelicopter);
            } else {
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    private void createBullet(Graphics g) {
        countBullet++;
        
        try {
            Image bullet = Image.createImage("/danminh1.png");
            playerBullet = new Sprite(bullet, 10, 22);
            playerBullet.setPosition(playerBulletX, playerBulletY);
            //playerBullet.setFrameSequence(frame);
            playerBullet.defineReferencePixel(5, 5);
            mLayerManager.append(playerBullet);
            System.out.println(countBullet);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public void run() {
        while (true) {
            try {
                //enemy1
//                if (co < 6) {
//                    createEnemy1(getGraphics());
//                    enemy1.setPosition(enemy1X, enemy1Y);
//                    enemy1Y = enemy1Y + 15;
//                }
//                if (co == 5) {
//                    enemy1Y = enemy1Y + 7;
//                    s[4].setPosition(enemy1X, enemy1Y);
//                    s[3].setPosition(enemy1X, enemy1Y);
//                    s[2].setPosition(enemy1X, enemy1Y);
//                    s[1].setPosition(enemy1X, enemy1Y);
//                    s[0].setPosition(enemy1X, enemy1Y);
//                }
//                if (co > 0) {
//                }
                //end enemy1

                //enemy helicopter 1
//                if (enemy1Y > getHeight()) {
//                    visibleHeli = true;
//                    if (count == 0) {
//                        Graphics g = getGraphics();
//                        createHelicopter(g);
//                    }
//                    if (count > 0) {
//                        helicopter1Y = helicopter1Y + 13;
//                        enemyhelicopter.setPosition(helicopter1X, helicopter1Y);
//                    }
//                }
                //end helicopter 1
                int ks = getKeyStates();
                //System.out.println(ks);
                int x1 = x;
                int y1 = y;
                
                if ((ks & FIRE_PRESSED) != 0) {
                    fire = true;

//                    createBullet(getGraphics());
//                    playerBullet.setPosition(playerBulletX, playerBulletY);
//                    playerBulletY = playerBulletY - 20;
                }
                if (fire == true) {
                    createBullet(getGraphics());
                    playerBullet.setPosition(playerBulletX, playerBulletY);
                    playerBulletY = playerBulletY - 20;
                }
                if ((ks & RIGHT_PRESSED) != 0) {
                    speed = 5;
                    playerBulletX = playerBulletX + speed;
                     playerBulletY = playerBulletY - 20;
                    if (fire == true) {
                        //createBullet(getGraphics());
                        playerBullet.setPosition(playerBulletX, playerBulletY);
                    }
                    if (x > getWidth() - 26) {
                        speed = 0;
                    }
                    startframe = 1;
                    x = x + speed;
                }
                if (ks == 0) {
                    startframe = 0;
                }
                if ((ks & LEFT_PRESSED) != 0) {
                    speed = 5;
                    if (fire == true) {
                    }
                    if (x < 1) {
                        speed = 0;
                    }
                    startframe = -1;
                    x = x - speed;
                }
                if ((ks & UP_PRESSED) != 0) {
                    speedY = 5;
                    if (y < 1) {
                        speedY = 0;
                    }
                    startframe = 1;
                    y = y - speedY;
                }
                if ((ks & DOWN_PRESSED) != 0) {
                    speedY = 5;
                    if (y > getHeight() - 26) {
                        speedY = 0;
                    }
                    startframe = 1;
                    y = y + speedY;
                }
                
                mPlayer.setPosition(x, y);
                mPlayer.setFrame(startframe + 1);
                
                
                
                Graphics g = getGraphics();
                g.setColor(255, 255, 255);
                g.fillRect(0, 0, w, h);
                mLayerManager.paint(g, 0, 0);
                flushGraphics();
                
                Thread.currentThread().sleep(150);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }
}
